<?php
 
namespace app\admin\validate;
use think\Validate; 
use app\admin\model\StudentCourse as StudentCourseModel;

class StudentCourse extends Validate{
    // 验证规则
    protected $rule = [
        ['student_id', 'require|number', '学员必须'],
        ['course_id', 'require|number|checkCourse', '课程必须选择|课程不正确|课程已选择'],
        ['end_time', 'require|dateFormat:Y-m-d H:i:s', '结束时间必须填写|时间格式不正确'],
        ['total_hours', 'require|number|egt:0', '总课时必须填写'],
        ['remain_hours', 'require|number|egt:0', '剩余课时必须填写|剩余课时不正确'],
//         ['remain_hours', 'require|number|<=:total_hours', '剩余课时必须填写|剩余课时不正确'],
    ];  

    // 自定义验证规则
    protected function checkCourse($value,$rule,$data)
    {
        if ($data['id']) {
            $info = StudentCourseModel::get($data['id']);
            if ($info['student_id'] == $data['student_id'] && $info['course_id']==$data['course_id']) {
                return true;
            }
        } 
        $info = StudentCourseModel::get(['student_id'=>$data['student_id'],'course_id'=>$data['course_id']]);
        return $info ? false : true;
        
    }
}